package com.ljfiction.book.fragment.transactionrecord;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.R;
import com.ljfiction.book.fragment.BaseFragment;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.ljfiction.book.tools.TimeTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by admin on 2017/9/22.
 */

public class TransectionPurseFragment extends BaseFragment {

    @BindView(R.id.myshupingrecycler)
    RecyclerView myshupingrecycler;
    Unbinder unbinder;
    @BindView(R.id.refresh_myshupingrecycle)
    SmartRefreshLayout refreshMyshupingrecycle;
    @BindView(R.id.zanwushuju)
    LinearLayout zanwushuju;
    private int page = 1;
    private List<PursBean.DataBean.ListBean> listData = new ArrayList<>();
    private BaseQuickAdapter transectionPurseAdapter;
    private PursBean myPursBean;
    private LinearLayout layout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myshuping_recycaler, null);
        unbinder = ButterKnife.bind(this, view);
        layout = view.findViewById(R.id.zanwushuju);
        intView();
        intData();
        return view;
    }

    private void intView() {
        myshupingrecycler.setAdapter(transectionPurseAdapter = new BaseQuickAdapter<PursBean.DataBean.ListBean, BaseViewHolder>(R.layout.mypurs_item, listData) {
            @Override
            protected void convert(BaseViewHolder helper, PursBean.DataBean.ListBean item) {
                AutoUtils.autoSize(helper.itemView);
                helper.setText(R.id.num_pursitem, "充值 " + String.valueOf(item.getMoney() * 100) + " 辣椒 / " + item.getMoney() + "元")
                        .setText(R.id.timepurs_item, TimeTool.getTimeAndChange(String.valueOf(item.getDateTimes())))
                        .setText(R.id.purse_type, StringTool.getType(item.getRechargeType()));
                if (item.getIsOK() == 0) {
                    helper.setText(R.id.ispursok, "充值失败");
                } else {
                    helper.setText(R.id.ispursok, "充值成功");
                }
            }
        });
        myshupingrecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        myshupingrecycler.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
        refreshMyshupingrecycle.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                if (myPursBean.getData().isIsLastPage()){
                    refreshlayout.finishLoadMore();
                    refreshMyshupingrecycle.setEnableLoadMore(false);
                }else {
                    page++;
                    intData();
                }

            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                listData.clear();
                refreshMyshupingrecycle.setEnableLoadMore(true);
                intData();
            }
        });
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/user-RechargeRecord")

                .params("page", page)
                .execute(new StringCallback() {

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        refreshMyshupingrecycle.finishLoadMore();
                        refreshMyshupingrecycle.finishRefresh();
                        Gson gson = new Gson();
                        try {
                            myPursBean = gson.fromJson(s, PursBean.class);
                            if (myPursBean.getReturnCode() == 200) {
                                myshupingrecycler.setVisibility(View.VISIBLE);
                                layout.setVisibility(View.GONE);
                                listData.addAll(myPursBean.getData().getList());
                                transectionPurseAdapter.notifyDataSetChanged();
                            } else if (myPursBean.getReturnCode() == 800&&page==1) {
                                myshupingrecycler.setVisibility(View.GONE);
                                layout.setVisibility(View.VISIBLE);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
